package com.intelligent.bot.api;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intelligent.bot.base.exception.E;
import com.intelligent.bot.base.result.B;
import com.intelligent.bot.model.Classify;
import com.intelligent.bot.model.Team;
import com.intelligent.bot.model.User;
import com.intelligent.bot.model.base.BaseDeleteEntity;
import com.intelligent.bot.model.req.AdminAddReq;
import com.intelligent.bot.model.req.AdminUpdateReq;
import com.intelligent.bot.model.req.LoginUserReq;
import com.intelligent.bot.model.req.UserGetListReq;
import com.intelligent.bot.model.res.ClassifyListRes;
import com.intelligent.bot.model.res.LoginUserRes;
import com.intelligent.bot.service.ITeamClassifyService;
import com.intelligent.bot.service.ITeamService;
import com.intelligent.bot.service.IUserService;
import com.intelligent.bot.utils.sys.JwtUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/sys/user")
@Log4j2
@Transactional(rollbackFor = E.class)
public class UserApi {

    @Resource
    IUserService userService;
    @Resource
    ITeamService teamService;

    @Resource
    ITeamClassifyService teamClassifyService;

    @RequestMapping(value = "admin/login",name = "登录")
    public B<LoginUserRes> adminLogin(@Validated @RequestBody LoginUserReq req) throws Exception {
        return userService.adminLogin(req);
    }

    @RequestMapping(value = "admin/add",name = "添加管理员")
    public B<Void> adminAdd(@Validated @RequestBody AdminAddReq req)  {
        return userService.adminAdd(req);
    }


    @RequestMapping(value = "/update",name = "修改账号密码")
    public B<Void> update(@Validated @RequestBody AdminUpdateReq req)  {
        return userService.update(req);
    }

    @RequestMapping(value = "getAdminList",name = "获取管理员列表")
    public B<Page<User>> getAdminList(@Validated @RequestBody UserGetListReq req)  {
        return userService.getAdminList(req);
    }

    @RequestMapping(value = "/getTeamList",name = "获取分组列表")
    public B<List<Team>> getTeamList()  {
        return B.okBuild(this.teamService.list());
    }

    @RequestMapping(value = "/delete",name = "删除用户")
    public B<Void> delete(@Validated @RequestBody BaseDeleteEntity req)  {
        this.userService.removeBatchByIds(req.getIds());
        return B.okBuild();
    }

    @RequestMapping(value = "/getClassifyList",name = "根据用户信息获取分类列表")
    public B<List<ClassifyListRes>> getClassifyList()  {
        User user = this.userService.getById(JwtUtil.getUserId());
        if(null == user || null == user.getTeamId()){
            return B.okBuild(new ArrayList<>());
        }
        List<Classify> classifyList = teamClassifyService.getClassifyList(user.getTeamId());
        if(classifyList.isEmpty()){
            return B.okBuild(new ArrayList<>());
        }
        List<ClassifyListRes> classifyListRes = BeanUtil.copyToList(classifyList, ClassifyListRes.class);
        return B.okBuild(TeamApi.buildTree(classifyListRes));
    }
}
